package com.ruoyi.web.domain.vo;

import java.util.HashMap;
import java.util.Objects;

/**
 * 统一 Ajax 返回对象
 *
 * @author ruoyi
 */
public class AjaxResult extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    /* 状态码 Key */
    public static final String CODE_TAG = "code";
    /* 返回数据 Key */
    public static final String DATA_TAG = "data";
    /* 返回消息 Key */
    public static final String MSG_TAG  = "msg";

    /* 状态码枚举 */
    public enum Type {
        /* 成功 */
        SUCCESS(200),
        /* 失败 */
        FAIL(500),
        /* 警告 */
        WARN(301),
        /* 错误 */
        ERROR(500);
        private final int value;
        Type(int value) {
            this.value = value;
        }
        public int value() {
            return this.value;
        }
    }

    /* ============================ 构造区域 ============================ */

    public AjaxResult() {
    }

    public AjaxResult(Type type, String msg) {
        super.put(CODE_TAG, type.value);
        super.put(MSG_TAG, msg);
    }

    public AjaxResult(Type type, String msg, Object data) {
        super.put(CODE_TAG, type.value);
        super.put(MSG_TAG, msg);
        if (Objects.nonNull(data)) {
            super.put(DATA_TAG, data);
        }
    }

    public static AjaxResult success() {
        return AjaxResult.success("操作成功");
    }

    public static AjaxResult success(Object data) {
        return AjaxResult.success("操作成功", data);
    }

    public static AjaxResult success(String msg) {
        return AjaxResult.success(msg, null);
    }

    public static AjaxResult success(String msg, Object data) {
        return new AjaxResult(Type.SUCCESS, msg, data);
    }

    public static AjaxResult fail() {
        return AjaxResult.fail("操作失败");
    }

    public static AjaxResult fail(String msg) {
        return AjaxResult.fail(msg, null);
    }

    public static AjaxResult fail(String msg, Object data) {
        return new AjaxResult(Type.FAIL, msg, data);
    }

    public static AjaxResult error() {
        return AjaxResult.error("未知异常");
    }

    public static AjaxResult error(String msg) {
        return AjaxResult.error(msg, null);
    }

    public static AjaxResult error(String msg, Object data) {
        return new AjaxResult(Type.ERROR, msg, data);
    }

    public static AjaxResult warn(String msg) {
        return AjaxResult.warn(msg, null);
    }

    public static AjaxResult warn(String msg, Object data) {
        return new AjaxResult(Type.WARN, msg, data);
    }

    /* ============================ 链式快捷区域 ============================ */

    /**
     * 自定义状态码
     */
    public AjaxResult setCode(int code) {
        super.put(CODE_TAG, code);
        return this;
    }

    /**
     * 自定义消息
     */
    public AjaxResult setMsg(String msg) {
        super.put(MSG_TAG, msg);
        return this;
    }

    /**
     * 放入数据（支持链式）
     */
    public AjaxResult setData(Object data) {
        super.put(DATA_TAG, data);
        return this;
    }

    /**
     * 放入任意键值（链式）
     */
    public AjaxResult put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    /* ============================ 快速获取区域 ============================ */

    public int getCode() {
        return Integer.parseInt(super.get(CODE_TAG).toString());
    }

    public String getMsg() {
        return super.get(MSG_TAG).toString();
    }

    public Object getData() {
        return super.get(DATA_TAG);
    }
}
